package com.kawa.kemanage.controller;

import com.kawa.kemanage.entity.UserSetting;
import com.kawa.kemanage.service.UserSettingService;
import com.kawa.kemanage.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/app/animation")
public class AnimationController {
    @Autowired
    private UserSettingService userSettingService;

    /**
     * 查询动画是否存在
     *
     * @param userId 用户Id
     * @return
     */
    @GetMapping("/haveAnimation")
    public Result queryAnimation(@RequestParam int userId) {
        try {
            if (userSettingService.queryAnimation(userId)) {
                return Result.success();
            } else {
                return Result.error("用户没有动画存在");
            }
        } catch (Exception e) {
            return Result.error("无法查询到动画信息：" + e.getMessage());
        }
    }

    /**
     * 查询动画列表
     *
     * @param userId 用户Id
     * @return
     */
    @RequestMapping("/animationList")
    public Result queryAnimationList(@RequestParam int userId) {
        try {
            Map<String, Object> resultHashMap = new HashMap<>();
            resultHashMap.put("List", userSettingService.queryAnimationList(userId));
            return Result.success(resultHashMap);
        } catch (Exception e) {
            return Result.error("获取动画列表失败" + e.getMessage());
        }

    }

    /**
     * 开启和关闭动画
     *
     * @param userId      用户id
     * @param enable      开启或关闭
     * @param animationId 动画id
     * @return
     */
    @RequestMapping("/setAnimation")
    public Result setAnimation(@RequestParam int userId, @RequestParam int enable, @RequestParam int animationId) {
        try {
            if (userSettingService.setAnimation(userId, enable, animationId)) {
                return Result.success();
            } else {
                return Result.error("修改失败");
            }

        } catch (Exception e) {

            return Result.error("修改失败" + e.getMessage());
        }

    }


}
